﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using CNShapes;

namespace CNPageViewer
{
    /// <summary>
    /// This class renders a FreehandShape.
    /// </summary><remarks>
    /// The freehand-shape holds a collection of lines. We get these lines, convert their
    /// coordinates to client coordinates for the viewer-control and draw them.
    /// </remarks>
    class ShapeRenderer_Freehand : IShapeRenderer
    {
        #region Public methods

        /// <summary>
        /// We draw the freehand shape passed in.
        /// </summary>
        public void renderShape(Shape shape, Graphics graphics, PageViewerControl pageViewerControl)
        {
            // We know that the shape is in fact a freehand shape, so we can cast it...
            Shape_Freehand freehandShape = (Shape_Freehand)shape;

            // We find the pen and make sure it's scaled correctly...
            Pen pen = RendererHelper.getPen(freehandShape, pageViewerControl);

            // We draw the line...
            drawLine(freehandShape.Line, pen, graphics, pageViewerControl);
        }

        /// <summary>
        /// Returns the Type we are managing.
        /// </summary>
        public Type getShapeType()
        {
            return typeof(Shape_Freehand);
        }

        #endregion

        #region Private functions

        /// <summary>
        /// We draw the line passed in, first translating the points into coordinates for the
        /// viewer-control.
        /// </summary>
        private void drawLine(IList<PointF> linePoints, Pen pen, Graphics graphics, PageViewerControl pageViewerControl)
        {
            // The line is a collection of points in shape-coordinates. We want to convert these into
            // client coordinates for the shape-viewer control...
            int iNumPoints = linePoints.Count;
            if (iNumPoints == 0)
            {
                return;
            }
            PointF[] clientPoints = new PointF[iNumPoints];
            for (int i = 0; i < iNumPoints; ++i)
            {
                clientPoints[i] = pageViewerControl.shapePointToClientPoint(linePoints[i]);
            }

            if (iNumPoints == 1)
            {
                // There is just one point, so we draw a dot...
                PointF point = clientPoints[0];
                PointF dotPoint = new PointF { X = point.X + 1, Y = point.Y };
                graphics.DrawLine(pen, point, dotPoint);
            }
            else
            {
                // There are multiple points so we can draw a line...
                graphics.DrawLines(pen, clientPoints);
            }
        }

        #endregion
    }
}
